package com.lap.auth.domain.dict.service;

import com.lap.auth.domain.dict.DictType;
import com.lap.auth.domain.dict.repository.DictTypeRepository;
import com.lap.auth.shared.errors.UserError;
import com.lap.framework.common.exception.BizException;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
public class DictTypeValidator {

  private final DictTypeRepository dictTypeRepository;

  public void validateCodeUnique(String code) {
    dictTypeRepository
        .findByCode(code)
        .ifPresent(dictType -> BizException.interrupt(UserError.DICT_TYPE_EXISTS));
  }

  public DictType assertDictTypeExists(Integer id) {
    return dictTypeRepository
        .findById(id)
        .orElseThrow(() -> BizException.newBiz(UserError.DICT_TYPE_NOTFOUND));
  }
}
